package mapeamentos.controle;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import mapeamentos.modelo.beans.TextoAula;
import mapeamentos.modelo.dao.ModuloDAO;
import mapeamentos.modelo.dao.TextoAulaDAO;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

public class UploadBean {

	private TextoAula textoAula = new TextoAula();
	private UploadedFile uploadedFile;
	
	public UploadedFile getUploadedFile() {
		return uploadedFile;
	}

	public void setUploadedFile(UploadedFile uploadedFile) {
		this.uploadedFile = uploadedFile;
	}

	public TextoAula getTextoAula() {
		return textoAula;
	}

	public void setTextoAula(TextoAula textoAula) {
		this.textoAula = textoAula;
	}

	public void salvar() {
//        UploadedFile uf = event.getFile();
        String nomeArquivo = this.uploadedFile.getFileName();
        File f = new File("C:\\Users\\Leandro\\Desktop\\eclipse-projeto\\workspace\\mapeamentos\\WebContent" + nomeArquivo);
        OutputStream os = null;
        InputStream is = null;
        try {
            is = this.uploadedFile.getInputstream();
            byte[] b = new byte[is.available()];
            os = new FileOutputStream(f);
            while (is.read(b) > 0) {
                os.write(b);
            }
            
            FacesMessage msg = new FacesMessage("Upload realizado com Sucesso!", this.uploadedFile.getFileName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (IOException ex) {
         //   Logger.getLogger(MbUpload.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                os.flush();
                os.close();
                is.close();
            } catch (IOException ex) {
               // Logger.getLogger(MbUpload.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
}
}
